﻿using DADAL;
using DABLL;
using SqlSugar;
using Microsoft.IdentityModel.Tokens;
using AntdUI;

namespace DataAcquisition
{
    public partial class DeviceManageForm : BaseForm
    {
        #region 属性
        /// <summary>
        /// key
        /// </summary>
        private string EncryptionKey = SqlSugarHelper.EncryptionKey;

        /// <summary>
        /// 当前页码
        /// </summary>
        private int PageNumber = 1;

        /// <summary>
        /// 每页数量
        /// </summary>
        private int PageSize = 10;

        /// <summary>
        /// 总数量
        /// </summary>
        private int TotalPages;

        #endregion

        #region 自定义方法

        /// <summary>
        /// 布局
        /// </summary>
        private void SetupForm()
        {
            Table1.Height = Height - Header.Height - StackPanel1.Height - Divider1.Height - Divider2.Height - Pagination1.Height - 5;
            Table1.Columns = new AntdUI.ColumnCollection {
                new AntdUI.Column("DeviceNumber","编号",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("DeviceName","名称",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("SlaveAddress","地址",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("PortAddress","串口",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("BaudRate","波特率",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("Operation","操作",AntdUI.ColumnAlign.Center){ Width="120", LineBreak=true}
            };

        }

        /// <summary>设备列表
        /// 
        /// </summary>
        public void LoadList()
        {
            List<DeviceModel> list = Device.DeviceList(PageNumber, PageSize, ref TotalPages);
            UpdateTable(list);
            Pagination1.Total = TotalPages;
            Pagination1.PageSize = PageSize;
        }

        /// <summary>更新Table
        /// 
        /// </summary>
        /// <param name="list"></param>
        private void UpdateTable(List<DeviceModel> list)
        {
            var antItemList = new List<DeviceItem>();
            foreach (DeviceModel item in list)
            {
                string decrypt = string.Empty;
                try
                {
                    decrypt = Encryption.DesDecrypt(item.SlaveAddress, EncryptionKey);
                }
                catch (Exception)
                {
                    Log.Error(string.Format("【{0}】设备地址加载失败", item.Name));
                }
                if (!decrypt.IsNullOrEmpty())
                {
                    antItemList.Add(new DeviceItem(item.Number,
                                               item.Name,
                                               Convert.ToInt16(decrypt),
                                               item.PortAddress,
                                               item.Baudrate));
                }
            }
            Table1.DataSource = antItemList;
        }

        #endregion

        #region 系统方法
        public DeviceManageForm()
        {
            InitializeComponent();
            Header.Text = "设备管理";
            FullScreen = true;
        }

        private void DeviceForm_Load(object sender, EventArgs e)
        {
            SetupForm();
            Table1.Spin("正在加载中...", (config) =>
            {
                LoadList();
            });
        }

        /// <summary>
        /// 换页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Pagination1_ValueChanged(object sender, AntdUI.PagePageEventArgs e)
        {
            PageNumber = e.Current;
            Task.Run(() =>
            {
                LoadList();
            });
        }

        /// <summary>
        /// 操作列
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Table1_CellButtonClick(object sender, AntdUI.TableButtonEventArgs e)
        {
            if (e.Record is DeviceItem data)
            {
                CellLink link = e.Btn;
                if (link.Id == "edit")
                {
                }
                else if (link.Id == "delete")
                {
                    string msg = string.Format("您确定要删除【{0}】设备吗？", data.DeviceName);
                    AntdUI.Modal.open(new AntdUI.Modal.Config(this, "友情提示", msg)
                    {
                        OnOk = config =>
                        {
                            Response response = Device.DeleteDevice(data.DeviceNumber);
                            if (response.Code != 200)
                            {
                                Error(response.Msg);
                            }
                            else
                            {
                                Success(response.Msg);
                                LoadList();
                            }
                            return true;
                        },
                    });
                }
                else if (link.Id == "point")
                {
                    DevicePointForm frm = new DevicePointForm();
                    frm.DeviceNumber = data.DeviceNumber;
                    frm.Owner = this;
                    frm.ShowDialog();
                }
            }
        }

        private void AddButton_Click(object sender, EventArgs e)
        {
            DeviceAddForm frm = new DeviceAddForm();
            frm.Owner = this;
            frm.ShowDialog();
        }

        #endregion
    }
}
